<?php
namespace app\appletadmin\controller;
use app\admin\controller\Common;
use think\Db;
use think\Log;
use app\service\propertyService;
class Carousel extends Common
{
	public function showlist()
	{
		$data=Db::table("applet_carousel")->select();
		$this->assign("data",$data);
		return $this->fetch();
	}

	public function edit()
	{
        //查询服务商信息
        $com = $this->pidTokenId();
        $where['id'] = array('in',$com['service_id']);
        $servicelist = (new propertyService())->serviceList($where);
        $this->assign('servicelist',$servicelist);

		if(request()->isPost())
		{
			//获取表单数据
            $data = request()->post();
            $file = request()->file('pic');
            //var_dump($data);
           	//var_dump(expression)
            if(!empty($file)){
                
               	$info = $file->rule('sha1')->move(ROOT_PATH . 'public' . DS . 'uploads'. DS .'icon');
                if($info){
                	$name=str_replace('\\', '/',$info->getSaveName());
                    $data['pic'] = "http://www.jfzhiwen.com/uploads/icon/".$name; 
                    //var_dump($data);die;
                }else{
                    echo $file->getError();
                    die;
                }  
            }
            $serviceinfo=Db::table("service")->where("id",$data["service_id"])->find();
           
            $data["service_name"]=$serviceinfo["name"];
            //将数据插入数据库
            $result = Db::table("applet_carousel")->update($data);;
            //判断数据是否插入成功
            if($result)
            {
                $this->success('修改成功',url('showlist'));
            }else
            {
                $this->error('修改失败',url('showlist'));
            }

		}else
		{
			$id=request()->get("id");
			$info=Db::table("applet_carousel")->where("id",$id)->find();
			$this->assign("info",$info);
			return $this->fetch();
		}	
	}
    public function edit2()
    {
        

        if(request()->isPost())
        {
            //获取表单数据
            $data = request()->post();
            $file = request()->file('pic');
            //var_dump($data);
            //var_dump(expression)
            if(!empty($file)){
                
                $info = $file->rule('sha1')->move(ROOT_PATH . 'public' . DS . 'uploads'. DS .'icon');
                if($info){
                    $name=str_replace('\\', '/',$info->getSaveName());
                    $data['pic'] = "http://www.jfzhiwen.com/uploads/icon/".$name; 
                    //var_dump($data);die;
                }else{
                    echo $file->getError();
                    die;
                }  
            }
           
            $result = Db::table("applet_carousel")->update($data);;
            //判断数据是否插入成功
            if($result)
            {
                $this->success('修改成功',url('showlist'));
            }else
            {
                $this->error('修改失败',url('showlist'));
            }

        }else
        {
            $id=request()->get("id");
            $info=Db::table("applet_carousel")->where("id",$id)->find();
            $this->assign("info",$info);
            return $this->fetch();
        }   
    }
    public function add()
    {
        //查询服务商信息
        $com = $this->pidTokenId();
        $where['id'] = array('in',$com['service_id']);
        $servicelist = (new propertyService())->serviceList($where);
        $this->assign('servicelist',$servicelist);
        if(request()->isPost())
        {
            //获取表单数据
            $data = request()->post();
            $file = request()->file('pic');
            //var_dump($data);
            //var_dump(expression)
            if(!empty($file)){
                
                $info = $file->rule('sha1')->move(ROOT_PATH . 'public' . DS . 'uploads'. DS .'icon');
                if($info){
                    $name=str_replace('\\', '/',$info->getSaveName());
                    $data['pic'] = "http://www.jfzhiwen.com/uploads/icon/".$name; 
                    //var_dump($data);die;
                }else{
                    echo $file->getError();
                    die;
                }  
            }
            $serviceinfo=Db::table("service")->where("id",$data["service_id"])->find();
           
            $data["service_name"]=$serviceinfo["name"];
            //将数据插入数据库
            $result = Db::table("applet_carousel")->insert($data);;
            //判断数据是否插入成功
            if($result)
            {
                $this->success('修改成功',url('showlist'));
            }else
            {
                $this->error('修改失败',url('showlist'));
            }

        }else
        {
            
            return $this->fetch();
        }   
    }
}